// 2023-7-10
class Promise {
    constructor(excutor) {
        this.promiseState = 'pending';
        this.promiseResult = undefined;
        this.callbacks = [];
        let onResolve = (val) => {
            if (this.promiseState !== 'pending') return;
            this.promiseState = 'fulfilled';
            this.promiseResult = val;
            this.callbacks.forEach(item => {
                item.success(val);
            })
        }
        let onReject = (rea) => {
            if (this.promiseState !== 'pending') return;
            this.promiseState = 'rejected';
            this.promiseResult = rea;
            this.callbacks.forEach(item => {
                item.fails(rea)
            })
        }
        try {
            excutor(onResolve, onReject);
        } catch (err) {
            onReject(err)
        }
    }
    then(onRes = val => val, onRej = rea => { throw rea }) {
        return new Promise((res, rej) => {

            let bundleFn = (fnName) => {
                    // 原生promise - 不是这么做的。
                    setTimeout(() => {
                        try {
                            let result = fnName(this.promiseResult);
                            // console.log(result);
                            // 非promise
                            // 抛错
                            if (result instanceof Promise) {
                                // 是promise
                                // console.log("是promise");
                                result.then(res, rej)
                            } else {
                                // console.log("不是promise");
                                res(result)
                            }
                        } catch (err) {
                            rej(err);
                        }
                    }, 0);
                }
                // 同步修改状态
            if (this.promiseState === 'fulfilled') {
                bundleFn(onRes)
            }
            if (this.promiseState === 'rejected') {
                bundleFn(onRej)
            }

            // 异步状态修改 - promiseState = pending
            if (this.promiseState === 'pending') {
                // 先将then的回调函数存起来 - 待日后调用
                this.callbacks.push({
                    success: () => {
                        bundleFn(onRes)
                    },
                    fails: () => {
                        bundleFn(onRej)
                    }
                });
            }
        })
    }
    catch (onRej) {
        return this.then(null, onRej);
    }
    static resolve(result) {
        return new Promise((res, rej) => {
            if (result instanceof Promise) {
                result.then(val => {
                    res(val);
                }, rea => {
                    rej(rea);
                })
            } else {
                res(result)
            }
        })
    }

    static reject(result) {
        return new Promise((res, rej) => {
            rej(result)
        })
    }

    // 
    static all() {

    }

    // 
    static race() {

    }
}